package api;

import java.io.IOException;
import java.util.Properties;

import redis.clients.jedis.Jedis;

/**
 * 商品相关接口
 * 
 * @author 毛建杰
 *
 */
public class GoodApi
{
	private static Properties properties;
	private static String redisIP;
	private static int redisPort;
	private static String redisPassword;
	private static int redisDb;

	static
	{
		try
		{
			properties = new Properties();
			properties.load(GoodApi.class.getResourceAsStream("/gjolshopapi.properties"));
			redisIP = properties.getProperty("redis.ip");
			redisPort = Integer.parseInt(properties.getProperty("redis.port"));
			redisPassword = properties.getProperty("redis.password");
			redisDb = Integer.parseInt(properties.getProperty("redis.db"));
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 游戏向平台请求商品列表接口
	 * 
	 * @param version
	 *            版本号
	 * @return 接口返回数据
	 * @throws Exception
	 *             异常
	 */
	public static String goodsList(String version) throws Exception
	{
		try (Jedis jedis = new Jedis(redisIP, redisPort))
		{
			jedis.auth(redisPassword);
			jedis.select(redisDb);
			String response = jedis.hget("goods_list", version);
			if (response == null || response.isEmpty())
			{
				throw new Exception("请求商品列表失败");
			}
			return response;
		}
		catch (Exception e)
		{
			throw e;
		}
	}

	public static void main(String[] args) throws Exception
	{
		String response = GoodApi.goodsList("1");
		System.out.println(response);
	}
}
